//
//  CommonTools.m
//  AAIM
//
//  Created by 远的小布丁 on 2019/12/17.
//  Copyright © 2019 AAIM. All rights reserved.
//

#import "CommonTools.h"

@implementation CommonTools
//判断是否全是空格

+(BOOL) isEmpty:(NSString *) str {
    if (!str) {
        return true;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
        
    }
    
}

#pragma 正则匹配手机号
+ (BOOL)isPhoneNumber:(NSString *)str
{
    if ([str length] == 0) {
         [MBProgressHUD showError:@"请输入手机号码" afterDelay:3.f];
        return NO;
    }
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:str];
    if (!isMatch) {
        [MBProgressHUD showError:@"请输入正确的手机号码" afterDelay:3.f];
        return NO;
    }
    return YES;
}

//是否为字符串
+ (BOOL) isBlankString:(NSString *)string {
    if (string == nil || string == NULL || [string isEqual: @"<null>"]) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}
//字符串是否为纯数字
+ (BOOL)isPureNumandCharacters:(NSString *)string
{
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(string.length > 0)
    {
            return NO;
        }
    return YES;
}


//直接调用这个方法就行
+(BOOL)checkIsHaveNumAndLetter:(NSString*)password{
    //数字条件
    NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
    
    //符合数字条件的有几个字节
    NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:password
                                                                       options:NSMatchingReportProgress
                                                                         range:NSMakeRange(0, password.length)];
    
    //英文字条件
    NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];
    
    //符合英文字条件的有几个字节
    NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];
    
    if (tNumMatchCount == password.length) {
        //全部符合数字，表示沒有英文
        return YES;
    } else if (tLetterMatchCount == password.length) {
        //全部符合英文，表示沒有数字
        return YES;
    } else if (tNumMatchCount + tLetterMatchCount == password.length) {
        //符合英文和符合数字条件的相加等于密码长度
        return YES;
    } else {
        return NO;
        //可能包含标点符号的情況，或是包含非英文的文字，这里再依照需求详细判断想呈现的错误
    }
    
}

+(BOOL)isNull:(NSDictionary *)dict key:(NSString*)key{
    // judge nil
    if(![dict objectForKey:key]){
        return NO;
    }
    
    id obj = [dict objectForKey:key];// judge NSNull
    
    BOOL isNull = [obj isEqual:[NSNull null]];
    return isNull;
}

+(void)changeNewFriendsStatus:(NSInteger)status withFriendID:(nonnull NSString *)friendID{
    NSString *savepath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

           //拼接文件路径
       NSString *filePathName = [savepath stringByAppendingPathComponent:[NSString stringWithFormat:@"historyNewFriendsList_%@.plist",UserFuuid]];
       NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePathName];
    NSMutableArray * dataArr = [NSMutableArray arrayWithArray:dataArray];
  __block NSMutableDictionary * changeDataDic;
//    NSString * 
    [dataArr enumerateObjectsUsingBlock:^(NSDictionary * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString * friendId = [NSString stringWithFormat:@"%@",obj[@"id"]];
        NSString * agreeStr = [NSString stringWithFormat:@"%@",obj[@"isAgree"]];
        if ([friendId isEqual:friendID]) {
            if ([agreeStr isEqualToString:@"1"]) {
                [dataArr removeObjectAtIndex:idx];
            }else{
                changeDataDic = [NSMutableDictionary dictionaryWithDictionary:obj];
                changeDataDic[@"isAgree"] = @"1";
                [dataArr replaceObjectAtIndex:idx withObject:changeDataDic];
            }
          
            [dataArr writeToFile:filePathName atomically:YES];
        }
        
   
    }];
}

+(NSString *)changeTimesTamp:(NSInteger)timesTamp{
    NSString * date = [NSDate dateStrFromCstampTime:timesTamp withDateFormat:@"yyyy/MM/dd"];
    return date;
}

//获取当前时间
+ (NSString *)currentDateStr{
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm"];//设定时间格式,这里可以设置成自己需要的格式
    NSString *dateString = [dateFormatter stringFromDate:currentDate];//将时间转化成字符串
    return dateString;
}

// 时间戳转时间,时间戳为13位是精确到毫秒的，10位精确到秒
+ (NSString *)getDateStringWithTimeStr:(NSString *)str{
    NSTimeInterval time=[str doubleValue]/1000;//传入的时间戳str如果是精确到毫秒的记得要/1000
    NSDate *detailDate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //实例化一个NSDateFormatter对象
    //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detailDate];
    return currentDateStr;
}

//获取当前时间戳
+ (NSString *)currentTimeStr{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}


 
@end
